<?php
/**
 * 管理员操作记录
 * @param $log_url 操作URL
 * @param $log_info 记录信息
 */
function adminLog($log_info)
{
    $add['log_time'] = time();
    $add['admin_id'] = session('admin_id');
    $add['log_info'] = $log_info;
    $add['log_ip'] = getIP();
    $add['log_url'] = __ACTION__;
    M('admin_log')->add($add);
}

function getAdminInfo($admin_id)
{
    return D('admin')->where("admin_id=$admin_id")->find();
}

/**
 * 面包屑导航  用于后台管理
 * 根据当前的控制器名称 和 action 方法
 */
function navigate_admin()
{
    $navigate = include APP_PATH . 'Common/Conf/navigate.php';
    $location = strtolower('Admin/' . CONTROLLER_NAME);
    $arr = array(
        '后台首页' => 'javascript:void();',
        $navigate[$location]['name'] => 'javascript:void();',
        $navigate[$location]['action'][ACTION_NAME] => 'javascript:void();',
    );
    return $arr;
}

/**
 * 导出excel
 * @param $strTable    表格内容
 * @param $filename 文件名
 */
function downloadExcel($strTable, $filename)
{
    header("Content-type: application/vnd.ms-excel");
    header("Content-Type: application/force-download");
    header("Content-Disposition: attachment; filename=" . $filename . "_" . date('Y-m-d') . ".xls");
    header('Expires:0');
    header('Pragma:public');
    echo '<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' . $strTable . '</html>';
}

/**
 * 格式化字节大小
 * @param  number $size 字节数
 * @param  string $delimiter 数字和单位分隔符
 * @return string            格式化后的带单位的大小
 */
function format_bytes($size, $delimiter = '')
{
    $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
    for ($i = 0; $size >= 1024 && $i < 5; $i++) $size /= 1024;
    return round($size, 2) . $delimiter . $units[$i];
}

/**
 * 根据id获取地区名字
 * @param $regionId id
 */
function getRegionName($regionId)
{
    $data = M('region')->where(array('id' => $regionId))->field('name')->find();
    return $data['name'];
}

function getMenuList($act_list)
{
    //根据角色权限过滤菜单
    $menu_list = getAllMenu();
    if ($act_list != 'all') {
        $right = M('system_menu')->where("id in ($act_list)")->cache(true)->getField('right', true);
        foreach ($right as $val) {
            $role_right .= $val . ',';
        }
        $role_right = explode(',', $role_right);
        foreach ($menu_list as $k => $mrr) {
            foreach ($mrr['sub_menu'] as $j => $v) {
                if (!in_array($v['control'] . 'Controller@' . $v['act'], $role_right)) {
                    unset($menu_list[$k]['sub_menu'][$j]);//过滤菜单
                }
            }
        }
    }
    return $menu_list;
}

function getAllMenu()
{
    return array(
        'system' => array('name' => '系统设置', 'icon' => 'fa-cog', 'sub_menu' => array(
            array('name' => '网站设置', 'act' => 'index', 'control' => 'System'),
            //array('name' => '权限资源列表', 'act' => 'right_list', 'control' => 'System'),
        )),
        'access' => array('name' => '权限管理', 'icon' => 'fa-gears', 'sub_menu' => array(
            array('name' => '管理员列表', 'act' => 'index', 'control' => 'Admin'),
            //array('name' => '角色管理', 'act' => 'role', 'control' => 'Admin'),
            array('name' => '管理员日志', 'act' => 'log', 'control' => 'Admin'),
        )),
        'goods' => array('name' => '商品管理', 'icon' => 'fa-book', 'sub_menu' => array(
            array('name' => '商品分类', 'act' => 'categoryList', 'control' => 'Goods'),
        )),
        'order' => array('name' => '评估管理', 'icon' => 'fa-money', 'sub_menu' => array(
           // array('name' => '估价选项', 'act' => 'categoryList', 'control' => 'Evaluation'),
            array('name' => '评估列表', 'act' => 'evaluationList', 'control' => 'Evaluation'),
        )),
        'content' => array('name' => '内容管理', 'icon'=>'fa-comments', 'sub_menu' => array(
            array('name' => '文章列表', 'act'=>'articleList', 'control'=>'Article'),
            array('name' => '文章分类', 'act'=>'categoryList', 'control'=>'Article'),
        )),
        'ad' => array('name' => '广告管理', 'icon' => 'fa-flag', 'sub_menu' => array(
            array('name' => '广告列表', 'act' => 'adList', 'control' => 'Ad'),
            array('name' => '广告位置', 'act' => 'positionList', 'control' => 'Ad'),
        )),
     /*   'tools' => array('name' => '插件工具', 'icon' => 'fa-plug', 'sub_menu' => array(
            array('name' => '数据备份', 'act' => 'index', 'control' => 'Tools'),
            array('name' => '数据还原', 'act' => 'restore', 'control' => 'Tools'),
        )),*/
    );
}

function respose($res)
{
    exit(json_encode($res));
}